﻿using System;
using System.Drawing;

using Storm.TextEditor.Win32;

namespace Storm.TextEditor.Drawing.GDI
{
	public class GDIBrush
		: GDIObject
	{
		#region Fields

		public    IntPtr brush       = IntPtr.Zero;
		protected bool   systemBrush = false;

		#endregion

		#region Methods

		#region Protected

		protected override void Destroy()
		{
			if (systemBrush == false)
			{
				if (brush != (IntPtr)0)
					NativeGdi32Api.DeleteObject(brush);
			}

			base.Destroy();
			brush = (IntPtr)0;
		}

		#endregion

		#endregion

		/// <summary>
		/// Initializes a new instance of GDIBrush.
		/// </summary>
		public GDIBrush(Color color)
		{
			brush = NativeGdi32Api.CreateSolidBrush(NativeUser32Api.ColorToInt(color));
			this.Create();
		}

		/// <summary>
		/// Initializes a new instance of GDIBrush.
		/// </summary>
		public GDIBrush(Bitmap pattern)
		{
			brush = NativeGdi32Api.CreatePatternBrush(pattern.GetHbitmap());
			this.Create();
		}

		/// <summary>
		/// Initializes a new instance of GDIBrush.
		/// </summary>
		public GDIBrush(IntPtr pattern)
		{
			brush = NativeGdi32Api.CreatePatternBrush(pattern);
			this.Create();
		}

		/// <summary>
		/// Initializes a new instance of GDIBrush.
		/// </summary>
		public GDIBrush(int style, Color color)
		{
			brush = NativeGdi32Api.CreateHatchBrush(style,
				NativeUser32Api.ColorToInt(color));

			this.Create();
		}

		/// <summary>
		/// Initializes a new instance of GDIBrush.
		/// </summary>
		public GDIBrush(int brushIndex)
		{
			brush = (IntPtr)brushIndex;
			systemBrush = true;

			this.Create();
		}
	}
}
